package com.dropdb.demo.controller.admin;


import com.dropdb.demo.bean.vo.BaseData;
import com.dropdb.demo.bean.Comment;
import com.dropdb.demo.bean.bo.AdminListCommentsBO;
import com.dropdb.demo.bean.vo.BaseRespVO;
import com.dropdb.demo.service.CommentService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("admin/comment")
public class AdminCommentController {
    @Autowired
    CommentService commentService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    @RequiresPermissions(value = {"*", "admin:comment:list"}, logical = Logical.OR)
    public BaseRespVO listComments(@Validated AdminListCommentsBO listCommentsBO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BaseRespVO.error(402, bindingResult.getFieldError().getDefaultMessage());
        }
        // 防止空字符串
        if (StringUtils.isEmpty(listCommentsBO.getUserId())) {
            listCommentsBO.setUserId(null);
        }
        if (StringUtils.isEmpty(listCommentsBO.getValueId())) {
            listCommentsBO.setValueId(null);
        }
        BaseData baseData = commentService.ListComments(listCommentsBO);
        return BaseRespVO.ok(baseData);
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @RequiresPermissions(value = {"*", "admin:comment:delete"}, logical = Logical.OR)
    public BaseRespVO deleteComment(@RequestBody Comment comment) {
        commentService.deleteComment(comment);
        return BaseRespVO.ok();
    }
}
